<template>
  <el-container>
    <!-- 头部模块 -->
    <el-header>
      <div>
        <img src="../assets/log.jpg" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout" class="logout">退出登录</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 左边导航栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="display" @click="toggleDisplay">
          ||||
        </div>
        <!-- 菜单栏模块 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#490Eff" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级导航栏 -->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <!-- 一级导航名称 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconfont[item.id]"> </i>
              <span> {{item.authName}}</span>
            </template>
            <!-- 二级导航 -->
            <el-menu-item :index=" '/'+ subItem.path+''" v-for="subItem in item.children" :key="subItem.id" @click="saveStatus(subItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边内容展示 -->
      <el-main>
        <router-view>

        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      iconfont: {
        '125': 'iconfont icon-touxiang',
        '103': 'iconfont icon-kongjian',
        '101': 'iconfont icon-gouwudai',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenu()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登录
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenu () {
      const { data: res } = await this.$axios.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(this.menulist);
    },
    toggleDisplay () {
      this.isCollapse = !this.isCollapse
    },
    saveStatus (path) {
      this.activePath = '/' + path
      sessionStorage.setItem('activePath', '/' + path)
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
// 头部区域
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #ffffff;
  font-size: 20px;
  div {
    display: flex;
    height: 100%;
    align-items: center;
    span {
      margin-left: 15px;
    }
    img {
      height: 100%;
    }
  }
}
// 左边导航栏
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
  .display{
    background-color: #4A5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    cursor: pointer;
    letter-spacing:.2em;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont{
  margin-right: 5px;
}

</style>
